<template>
  <div>
    <v-edit :title="init?.isAdd?'设备新增':'设备编辑'" :init="comm" :infos="info" @reset="reset" @save="save"
            @close="close" :rules="rules">
      <template #formItem>
        <div class="two-col intro-y">
          <wde-input label="设备编号" prop="equtCode" v-model="info.equtCode"/>
          <wde-switch label="停用" v-model="info.stopFlag"/>
        </div>
        <div class="one-col intro-y">
          <wde-input label="设备名称" prop="equtName" v-model="info.equtName"/>
        </div>
        <div class="two-col intro-y">
          <wde-input label="装机容量" prop="userName" v-model="info.installCapacity"/>
          <wde-select label="机组状态" v-model="info.equtState" :options="equtStateList"/>
        </div>
        <div class="two-col intro-y">
          <wde-number label="冷却启动费用(元/次)" v-model="info.coldStartCost"/>
          <wde-number label="冷却启动时间(分钟)" v-model="info.coldStartTime"/>
        </div>
        <div class="two-col intro-y">
          <wde-number label="热态启动费用(元/次)" v-model="info.hotStartCost"/>
          <wde-number label="热态启动时间(分钟)" v-model="info.hotStartTime"/>
        </div>
        <div class="two-col intro-y">
          <wde-number label="温态启动费用(元/次)" v-model="info.warmStartCost"/>
          <wde-number label="温态启动时间(分钟)" v-model="info.warmStartTime"/>
        </div>
        <div class="two-col intro-y">
          <wde-number label="最小连续停机时间(分钟)" v-model="info.minStopTime"/>
          <wde-number label="最小连续运行时间(分钟)" v-model="info.minRunTime"/>
        </div>
        <div class="two-col intro-y">
          <wde-number label="空载费用(元/小时)" v-model="info.idleCost"/>
          <wde-number label="日最大启停数" v-model="info.dayShutdownCount"/>
        </div>
        <div class="two-col intro-y">
          <wde-number label="停机至冷态时间(分钟)" v-model="info.stopColdTime"/>
          <wde-number label="停机至温态时间(分钟)" v-model="info.stopWarmTime"/>
        </div>
        <div class="two-col intro-y">
          <wde-number label="上调爬坡速率(MW/min)" v-model="info.upRate"/>
          <wde-number label="下调爬坡速率(MW/min)" v-model="info.downRate"/>
        </div>
        <div class="two-col intro-y">
          <wde-number label="启动通知时间(小时)" v-model="info.startNotifyTime"/>
          <wde-number label="最小发电成本(元/MWh)" v-model="info.minWorkCost"/>
        </div>
        <!-- <div class="two-col intro-y">
          <wde-price label="金额(万元)" v-model="info.price" />
        </div>
        <div class="two-col intro-y">
          <wde-muti-select label="等级" v-model="info.levels" :options="options" />
        </div>
        <div class="two-col intro-y">
          <wde-select label="项目类型" v-model="info.type" :options="options" />
          <wde-muti-select label="等级" v-model="info.levels" :options="options" />
        </div>
        <div class="two-col intro-y">
          <wde-date label="计划开始时间" dateType="date" v-model="info.date" />
        </div>
        <div class="two-col intro-y">
          <wde-switch label="是否激活" v-model="info.isActive" />
        </div> -->
      </template>
    </v-edit>
  </div>
</template>

<script setup>
import {ref, toRefs, watch, getCurrentInstance, onMounted} from "vue";
import {useEditLogic} from "@/views/base/useEditLogic";
import * as $api from "@/api/api.js";
import {ElMessage} from 'element-plus'
import {EquipmentInsert, EquipmentUpdate, ReferDict} from "@/api/api.js";

const {proxy} = getCurrentInstance();
const emit = defineEmits(["close"]);

const props = defineProps({
  args: {
    type: Object,
    default: () => null,
  },
  init: {
    type: Object,
    default: () => null,
  },
});
// 使用 toRefs 解构 props
const {init} = toRefs(props);
const info = ref({
  equtKey: "",
  equtCode: "",
  equtName: "",
  installCapacity: 0,
  stopFlag: true,
  coldStartCost: 0,
  coldStartTime: 0,
  hotStartCost: 0,
  hotStartTime: 0,
  warmStartCost: 0,
  warmStartTime: 0,
  minStopTime: 0,
  minRunTime: 0,
  idleCost: 0,
  dayShutdownCount: 0,
  equtState: 0,
  stopColdTime: 0,
  stopWarmTime: 0,
  upRate: 0,
  downRate: 0,
  startNotifyTime: 0,
  minWorkCost: 0
});
const userKey = ref(localStorage.getItem("userKey"));
const repeatPwd = ref("");
// const info = ref({
//   tchKey: "",
//   tchNum: "",
//   tchName: "",
//   sex: true,
//   tel: "",
//   email: "",
//   title: "",
//   birthday: "",
//   nation: "",
//   idType: "",
//   idCode: "",
//   stopFlag: true,
//   remark: "",
// });
const equtStateList = ref([]);
const rules = ref({
  equtCode: [
    {required: true, message: "请输入设备编号", trigger: "blur"},
  ],
  equtName: [
    {required: true, message: "请输入设备名称", trigger: "blur"},
  ],
  userPwd: [
    {required: true, message: "请输入密码", trigger: "blur"},
  ],
  repeatPwd: [
    {required: true, message: "请输入确认密码", trigger: "blur"},
  ]
});
onMounted(
    () => {
      getEquiStateList();
    }
)
const getEquiStateList = () => {
  $api.ReferDict({
    plantKey: "",
    userKey: userKey.value,
    key: "100"
  }).then((res) => {
    equtStateList.value = res.subs.map(item => {
      return {
        value: item.dictID,
        label: item.dictName,
      }
    });
  });
};
const buildAdd = () => {
  return $api.EquipmentInsert(info.value);
  // 构建新增逻辑
};

const buildUpdate = () => {
  return $api.EquipmentUpdate(info.value);
  // return $api.BATeacherUpdate(info.value);
};
const resetAdd = () => {
  proxy.$propsEmpty(info.value);
  // proxy.$propsCopy(info.value, props.init);
};
const resetUpdate = () => {
  proxy.$propsCopy(info.value, props.init);
};
const clearInfo = () => {
  proxy.$propsEmpty(info.value);
};

const close = () => {
  emit("close");
};
const {loading, comm, isView, reset, save, VEdit} = useEditLogic(
    init,
    info,
    resetAdd,
    resetUpdate,
    clearInfo,
    buildAdd,
    buildUpdate,
    close
);
watch(
    () => comm,
    (newVal) => {
      console.log(newVal);
    }
);
watch(
    () => props.init,
    (newVal) => {
      if (newVal) repeatPwd.value = newVal.userPwd;
    }
);
</script>

<style></style>